
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

# Data for Table_A4
data = {'Effort': [0.064, -0.273, 0.013, 0.101, -0.069], 'Reliable': [0.051, -0.189, 0.01, 0.056, -0.067], 'Responsive': [0.236, -0.151, -0.041, 0.092, -0.08], 'Experience': [0.211, -0.151, 0.01, -0.04, -0.083], 'Creative': [0.057, -0.139, 0.045, 0.046, -0.034], 'Trustworthy': [0.151, -0.244, 0.066, -0.047, -0.01], 'Approach': [0.193, -0.153, 0.068, -0.076, -0.112], 'Confident': [0.157, -0.215, 0.028, -0.052, -0.022], 'Empathy': [0.173, -0.152, -0.077, -0.007, -0.15], 'Overall': [0.003, -0.197, 0.145, 0.02, -0.057]}

# Standard errors
std_errors = {'Effort': [0.074, 0.095, 0.096, 0.095, 0.091], 'Reliable': [0.077, 0.101, 0.102, 0.098, 0.087], 'Responsive': [0.095, 0.099, 0.1, 0.097, 0.087], 'Experience': [0.118, 0.118, 0.139, 0.134, 0.096], 'Creative': [0.112, 0.133, 0.132, 0.123, 0.102], 'Trustworthy': [0.086, 0.117, 0.15, 0.11, 0.097], 'Approach': [0.091, 0.092, 0.096, 0.09, 0.084], 'Confident': [0.084, 0.088, 0.097, 0.091, 0.078], 'Empathy': [0.098, 0.105, 0.115, 0.109, 0.097], 'Overall': [0.094, 0.089, 0.108, 0.105, 0.088]}

# Independent variable labels
iv_labels = ['Extraversion', 'Agreeableness', 'Conscientiousness', 'Neuroticism (Reverse-coded)', 'Openness']

# Convert the data and standard errors to DataFrames
df = pd.DataFrame(data, index=iv_labels)
df_se = pd.DataFrame(std_errors, index=iv_labels)

# Function to calculate T-values from coefficients and standard errors
def calculate_t_values(df, df_se):
    return df / df_se

# Calculate T-values
t_values_df = calculate_t_values(df, df_se)

# Generate significance annotations based on T-values
annotations = t_values_df.applymap(lambda x: '***' if abs(x) > 2.576 else '**' if abs(x) > 1.96 else '*' if abs(x) > 1.645 else '')

# Plotting the heatmap
plt.figure(figsize=(12, 6))

# Plot the heatmap with significance annotations and black-and-white gradient
ax = sns.heatmap(t_values_df.astype(float), annot=annotations, fmt="", cmap="Greys", center=0,
                 linewidths=0.5, cbar_kws={'label': 'T-values', 'orientation': 'horizontal'}, mask=t_values_df.isnull())

# Adjust labels and angles
ax.xaxis.tick_top()
ax.xaxis.set_label_position('top')
ax.set_xticklabels(t_values_df.columns, rotation=45, ha="center", fontsize=10)
ax.set_yticklabels(t_values_df.index, rotation=0, fontsize=10)

# Position the color bar (legend) at the bottom
cbar = ax.collections[0].colorbar
cbar.ax.xaxis.set_label_position('bottom')
cbar.ax.xaxis.set_ticks_position('bottom')

# Adjust layout
plt.tight_layout()
plt.show()
